import Sequelize from 'sequelize';
import sequelizeDB from "../db/user";
const sequelize = sequelizeDB(Sequelize);
import userM from '../model/user.js';
const um = userM(sequelize, Sequelize);

var constant = require('../utils/constant');

var crypto = require('crypto'); //md5 加密
var jwt = require('jsonwebtoken'); //token模块


class userC {
	constructor(x, y,z) {
		this.sequelize = x;
		this.Sequelize = y;
		this.um=z;
	}
	//创造新的表
	createtable = (req, res, next) => {
		um.createTable().then(result => {
		    res.json({
		        code: constant.RESULT.SUCCESS.code,
		        msg: constant.RESULT.SUCCESS.msg,
		        data: '创建表成功'
		    });
		}).catch(err => {
		    res.json({
		        code: constant.RESULT.NO_DATA.code,
		        msg: constant.RESULT.NO_DATA.msg,
		        data: err
		    });
		})
	}
	//创建用户
	createuser(req, res, next) {
	    let username = req.query.username;
		//let username = 'hello2';
	    //密码md5加密
	    let password = crypto.createHash('md5').update(username).digest('hex');
	    //创建用户时创建token
	    let token = jwt.sign({username: username}, constant.jwtsecret, {
	        expiresIn: "7d"  // 一周过期
	    })
	    let user = {
	        username: username,
	        password: password,
	        age: 10,
	        token: token
	    }
	    um.userCreate(user).then(result => {
	        res.json({
	            code: constant.RESULT.SUCCESS.code,
	            msg: constant.RESULT.SUCCESS.msg,
	            data: '创建成功'
	        });
	    }).catch(err => {
	        res.json({
	            code: constant.RESULT.NO_DATA.code,
	            msg: constant.RESULT.NO_DATA.msg,
	            data: err
	        });
	    })
	}
	//删除用户
	deleteuser(req, res, next) {
	    let username = req.query.username
	
	    um.userDelete({
	        where: {
	            username: username
	        }
	    }).then(result => {
	        res.json({
	            code: constant.RESULT.SUCCESS.code,
	            msg: constant.RESULT.SUCCESS.msg,
	            data: '删除成功'
	        });
	    }).catch(err => {
	        res.json({
	            code: constant.RESULT.NO_DATA.code,
	            msg: constant.RESULT.NO_DATA.msg,
	            data: err
	        });
	    })
	}
	
	//更新用户信息
	updateuser(req, res, next) {
	    let username = req.query.username
	    let newDate = {
	        username: '11111',
	        age: 14
	    }
	    um.userUpdate(newDate, {
	        where: {
	            username: username
	        }
	    }).then(result => {
	        res.json({
	            code: constant.RESULT.SUCCESS.code,
	            msg: constant.RESULT.SUCCESS.msg,
	            data: '更新成功'
	        });
	    }).catch(err => {
	        res.json({
	            code: constant.RESULT.NO_DATA.code,
	            msg: constant.RESULT.NO_DATA.msg,
	            data: err
	        });
	    })
	}
	
	//条件查询数据
	queryuser(req, res, next) {
	    let username = req.query.username;
	    um.userFindOne({
	        where: {
	            [Op.and]: [{username: username}, {age: 10}]//op 条件筛选 具体用法查看文档
	        }
	    }).then(userinfo => {
	        res.json({
	            code: constant.RESULT.SUCCESS.code,
	            msg: constant.RESULT.SUCCESS.msg,
	            data: userinfo
	        });
	    }).catch(err => {
	        res.json({
	            code: constant.RESULT.NO_DATA.code,
	            msg: constant.RESULT.NO_DATA.msg,
	            data: err
	        });
	    })
	}
}


module.exports = new userC(sequelize, Sequelize,um);
